package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.utils.ObjectToMapConverter;
import com.yunyao.dao.mapper.BamuSpReconciliationSettlementOrderMaterialMapper;
import com.yunyao.dao.model.BamuSpReconciliationSettlementOrderMaterial;
import com.yunyao.dao.model.ReconciliationSettlementOrderMaterialSum;
import com.yunyao.dao.service.bamu.IBamuSpReconciliationSettlementOrderMaterialService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供应商对账结算订单材料表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuSpReconciliationSettlementOrderMaterialServiceImpl extends ServiceImpl<BamuSpReconciliationSettlementOrderMaterialMapper, BamuSpReconciliationSettlementOrderMaterial> implements IBamuSpReconciliationSettlementOrderMaterialService {

    @Override
    public void deletedByReconciliationIdList(List<String> reconciliationIdList) {
        // 检查对账ID列表是否为空，如果为空则直接返回空列表
        if (CollectionUtils.isEmpty(reconciliationIdList)) {
            return;
        }

        // 创建查询条件封装Lambda表达式，用于筛选特定条件的对账结算单
        LambdaUpdateWrapper<BamuSpReconciliationSettlementOrderMaterial> wrapper =
                new LambdaUpdateWrapper<BamuSpReconciliationSettlementOrderMaterial>()
                        // 条件1：对账ID在给定的列表中
                        .in(BamuSpReconciliationSettlementOrderMaterial::getReconciliationId,
                                reconciliationIdList)
                        .eq(BamuSpReconciliationSettlementOrderMaterial::getIsDeleted,
                                DataDeletedStatus.NO.getType())
                        .set(BamuSpReconciliationSettlementOrderMaterial::getIsDeleted,
                                DataDeletedStatus.YES.getType());
        update(wrapper);
    }

    @Override
    public Map<String, Object> getMaterialSumMap(String reconciliationSettlementId) {
        if (StringUtils.isBlank(reconciliationSettlementId)) {
            return Map.of();
        }
        ReconciliationSettlementOrderMaterialSum materialSum =
                baseMapper.getMaterialSum(reconciliationSettlementId);
        try {
            return ObjectToMapConverter.toMap(materialSum);
        } catch (IllegalAccessException e) {
            return Map.of();
        }
    }
}
